package com.budwk.app.popo;

import com.alibaba.fastjson.JSONObject;
import com.budwk.app.popo.vo.PopoMessageContentVO;
import lombok.Data;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.Date;

/**
 * @author ZY.Wang
 * @since 2023-05-10
 */
public class PopoUtils {

    /**
     * 获取泡泡鉴权url
     *
     * @param url url
     * @return 结果
     */
    public static String getPoPoTokenUrl(String url) {
        if (StringUtils.isBlank(url)) return null;
        return getPoPoTokenCheck(url).getUrlWithToken();
    }

    /**
     * 获取泡泡鉴权对象
     *
     * @param url 接口url
     * @return 结果
     */
    public static PoPoTokenCheck getPoPoTokenCheck(String url) {
        String apiSecret = "5cd7e9aaf6d0cff121e0a41257246392";
        String timeStampStr = (new Date().getTime() / 1000L) + "";
        String popTokenPreStr = timeStampStr + apiSecret;

        String popToken = DigestUtils.md5Hex(popTokenPreStr);

        PoPoTokenCheck poPoTokenCheck = new PoPoTokenCheck();
        poPoTokenCheck.setPopToken(popToken);
        poPoTokenCheck.setTimestamp(timeStampStr);

        if (StringUtils.isNotBlank(url)) {
            String tokenUrl = url + "?timestamp=" + timeStampStr + "&pop_token=" + popToken;
            poPoTokenCheck.setUrlWithToken(tokenUrl);
        }

        return poPoTokenCheck;
    }

    public static String getServiceOrderPopoContent(String serviceOrderId) {
        return "serviceOrder[" + serviceOrderId + "]";
    }

    public static String getServiceOrderPopContent(PopoMessageContentVO messageContentVO) {
        String msgPrefix = "serviceOrder[" + messageContentVO.getServiceOrderId() + "]";
        String msgStr = JSONObject.toJSONString(messageContentVO);
        return msgPrefix + msgStr;
    }

    @Data
    public static class PoPoTokenCheck {
        private String timestamp;
        private String popToken;
        private String urlWithToken;
    }

    public static void main(String[] args) {
        System.out.println(getPoPoTokenCheck(null));
    }
}
